// pages/list/detail/detail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    autoplay: true,
    interval: 3000,
    duration: 1200,
    current:1,//轮播图当前状态
    pic_num:0,//轮播图总数
    tab_num:1,//详情评论切换
    floorstatus:0,//是否显示回到顶部按钮
    good:{},//商品数据
    pro_num:1,
    img_url:'https://rt.gzziyu.com/',
    pllist:[],
    good_id:'',
    lunboData:[],
    xiang_img:'',
    attr1: {},
    attr2: {},
    attr3:{},
    this_price:0,
    spec_list:[],
    good_price:0

  },

 
  plList: function () {
    let that = this
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=hangqingpinglunlist',
      data: {
        goods_id: 315
      },
      success: function (res) {
        console.log(res)
        that.setData({
          pllist: res.data
        })
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].user_name == '') {
            res.data[i].user_name = '匿名评论'
            that.setData({
              pllist: res.data
            })
          }
        }
      }
    })
  },
  plC: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  sendPl: function () {
    let that = this;

    let  id = that.data.good_id;
    wx.navigateTo({
      url: '../../release_evaluation/release_evaluation?id='+id
    });
   
  },
   /**
   * 生命周期函数--监听页面加载
   */
  jisuan_price: function () {
    var attr = this.data.attr3;
    var good_price = Number(this.data.good_price) ? Number(this.data.good_price):0;
    var price = 0;

    for (var item1 in attr) {
      for (var item2 in attr[item1].values) {
        
        if (attr[item1].values[item2].checked == '1') {
          
          price = price + Number(attr[item1].values[item2].format_price)
          console.log('价格:' + attr[item1].values[item2].format_price)
          console.log(price)

        }
      }
    }
    console.log(price, good_price)
    var allprice = price + good_price;
    console.log(allprice)
    this.setData({
      this_price: price + good_price
    })
  },

  change_attr:function(e){
    var allindex = e.target.dataset.allindex;
    var oneindex = e.target.dataset.oneindex;
    var id = e.target.dataset.id;
    var attr = this.data.attr3;
    for (var index in attr[allindex].values) {
      attr[allindex].values[index].checked = '0';
    }
    attr[allindex].values[oneindex].checked = '1';
    this.setData({
      attr_id_3: e.target.dataset.id,
      attr3: attr
    })


    this.jisuan_price();
  },
 
  onLoad: function (options) {
    var that = this;
    console.log(options)
    var goods_id = options.id ? options.id :"348"
    // 商品详情
    wx.request({
      url: 'https://rt.gzziyu.com//mobile/ny_flow_goods.php?action=detail',
      data:{
        goods_id: goods_id
      },
      async: true,
      success:function(res){
        console.log(res)
        var xiang_img = res.data.goods_desc;
        var index = xiang_img.replace(/\<img/gi, '<img class="rich-img"');
        console.log(index)
        if(res.data){
          that.setData({
            good: res.data,
            good_price: res.data.shop_price,
            lunboData: res.data.gallery,
            pic_num: res.data.gallery.length,
            good_id: goods_id,
            xiang_img: index,
            xiang_img1: res.data.goods_desc
          })
         
        }else{
          return false
        }
        that.jisuan_price();

        // 评论列表
        wx.request({
          url: 'https://rt.gzziyu.com//mobile/ziyu.php?Action=hangqingpinglunlist',
          data: {
            goods_id:res.data.goods_id
          },
          header: {
            "Content-Type":"application/json"
          },
          success:function(res){
            console.log(res)
            that.setData({
              pllist:res.data
            })
          }
        })
      }
    })
    
    // 商品attr
    wx.request({
      url: 'https://rt.gzziyu.com/mobile/ny_flow_goods.php?action=properties&goods_id=' + goods_id,
      success:function(res){
        console.log(res)
        console.log(res.data.lnk)
        console.log(res.data.pro.商品属性)
        console.log(res.data.spe);
        var data = res.data.spe;
        var spec_list = that.data.spec_list;

        for (var item in data){
          var arr = data[item].values;
          console.log(arr);
          var obj= {};
          obj.id = item;
          obj.value = '';
          console.log(obj)
          spec_list.push(obj)
          for (var index in arr) {
            arr[index].checked = 0;
          }
          arr[0].checked = 1;
          data[item].values = arr;
        }
        console.log(spec_list)

        that.setData({
          attr1: res.data.lnk,
          attr2: res.data.pro.商品属性,
          attr3: data,
          spec_list: spec_list
        })
        console.log(spec_list);
        that.jisuan_price();
      }
    })
   


  },
  // 轮播图切换
  changeswiper:function(e){
    this.setData({
      current: e.detail.current+1
    })
  },
  // 加入购物车
  addCar: function () {
    var proId = this.data.good.goods_id;
    var that = this;
    var list=[];
    var attr = this.data.attr3;
    for(var item1 in attr){
      for(var item2 in attr[item1].values){
        if (attr[item1].values[item2].checked == '1'){
          console.log(attr[item1].name+':'+ attr[item1].values[item2].id)
          list.push(attr[item1].values[item2].id)
        }
      }
    }
   
    list = list.join(',');
    console.log(list)
    let obj = {
      openid: wx.getStorageSync('opid'),
      goods_id: proId,
      goods_number: this.data.pro_num,
      spec_list: list
    }
    console.log(obj)
    var bool = true;

    wx.request({
      url: app.globalData.url + 'ny_flow_cart.php?action=add_to_cart',
      data: obj,
      anysc:true,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log('加入购物车')
        console.log(res)
        if(res.data.msg == "success"){
          wx.showToast({
            title: '加入购物车成功',
            icon: 'none',
            mask: true,
          })
        }else{
          bool = false
          wx.showToast({
            title: '加入失败',
            icon: 'none',
            mask: true,
          })

         
        }
      }
    })
    return bool
    

  },
  // 详情与评论切换
  change_tab:function(e){
    console.log(e.target.dataset.id)
    this.setData({
      tab_num: e.target.dataset.id
    })
  },
  // 加
  add_num:function(){
    const num = this.data.pro_num
    if(num >= 0){
      this.setData({
        pro_num: num+1
      })
    }
  },
  // 改变
  num: function (e) {

    this.setData({
      pro_num: e.detail.value
    })
  },
  // 减
  min_num:function(){
    const num = this.data.pro_num
    if (num > 0) {
      this.setData({
        pro_num: num - 1
      })
    }
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  goTop: function (e) {  // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  // 立即购买
  toCar: function () {
   var bool =  this.addCar();
   wx.showLoading({
     title: '加载中',
   })
    setTimeout(function(){
      wx.switchTab({
        url: '../../cart/cart'
      })
      wx.hideLoading()
    },1000)
     
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})